<!--
 * @Author: fangshengchu
 * @Date: 2024-05-09 15:30:47
 * @LastEditTime: 2024-06-21 14:50:42
 * @LastEditors: fangshengchu
 * @Description: 
 * @FilePath: \szhk-fe\src\views\purchaseArrival\arrivalRegistration\SelectOrderModal.vue
-->
<template>
  <contentModel
    title="添加到货订单"
    :has-footer="true"
    :no-header-border="true"
    @close="cancel"
  >
    <OrderDetail @select="changeSelect"></OrderDetail>
    <template #footer>
      <a-space>
        <a-button type="outline" @click="handleCancel(record, rowIndex)"
          >取消</a-button
        >
        <a-button
          type="primary"
          :loading="loading"
          @click="handleSubmit(record, rowIndex)"
          >保存</a-button
        >
      </a-space>
    </template>
  </contentModel>
</template>

<script lang="ts" setup>
  import { Message } from '@arco-design/web-vue';
  import { defineAsyncComponent, inject, ref, toRefs } from 'vue';
  import { saveAllPmsReceiveDetail } from '@/api/purchaseArrival/arrivalRegistration';
  import OrderDetail from './OrderDetail.vue';

  const contentModel = defineAsyncComponent(
    () => import('@/common/components/content-model/index.vue')
  );

  const props = defineProps({
    modelValue: {
      type: Boolean,
      default: false,
    },
    // eslint-disable-next-line vue/require-prop-types
    pmsReceiveId: {},
    // eslint-disable-next-line vue/require-prop-types
    boxSelectId: {},
  });
  const emits = defineEmits(['update:modelValue', 'fetchData']);
  const cancel = () => {
    emits('update:modelValue', false);
  };
  const loading = ref(false);
  const { pmsReceiveId } = toRefs(props);
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  let selectList = [];
  const changeSelect = (rows) => {
    selectList = rows || [];
  };
  const handleSubmit = async () => {
    if (!selectList.length) {
      Message.info({
        content: '请选择到货订单！',
        duration: 5 * 1000,
      });
      return;
    }
    if (!selectList.every((item) => item.urgencyDegree)) {
      Message.info({
        content: '请编辑数据后再保存！',
        duration: 5 * 1000,
      });
      return;
    }
    try {
      loading.value = true;
      const form = formateForm(selectList);
      await saveAllPmsReceiveDetail(form);
      Message.success({
        content: '保存成功！',
        duration: 5 * 1000,
      });
      emits('fetchData');
      cancel();
    } catch (error) {
      loading.value = false;
    }
  };
  const pmsReceiveRow = inject('pmsReceiveRow');

  // 格式化入参
  const formateForm = (list) => {
    // 需要变更的key 订单表key: 装箱明细表key
    const keyMap = {
      // pmsDeliveryQty: 'pmsDeliverQty',
      // invDeliveryQty: 'invDeliverQty',
      marque: 'modelMark',
      parentModelNo: 'parentModel',
      specs: 'specification',
    };
    let form = [];
    form = list.map((item) => {
      const formItem = {
        ...item,
        // eslint-disable-next-line object-shorthand
        pmsReceiveId: pmsReceiveId?.value,
        mdsVendorId: pmsReceiveRow?.value.mdsVendorId,
        // pmsReceiveBoxId: boxSelectId?.value,
        pmsOrderLId: item.id,
        pmsOrderId: item.pmsOrderId,
        confirmFlag: 0,
        batchFlag: 0,
        receiveType: pmsReceiveRow?.value.deliverType,
        secretLevel: pmsReceiveRow?.value.secretLevel,
      };
      Object.keys(keyMap).forEach((key) => {
        formItem[keyMap[key]] = formItem[key];
      });
      delete formItem.id;
      delete formItem.updater;
      delete formItem.updateId;
      return formItem;
    });
    return form;
  };
  const handleCancel = () => {
    cancel();
  };
</script>

<style lang="less" scoped>
  @import url('../moduleCommon/common.less');
</style>
